<template>
  <el-dialog v-model="dialogVisible" title="表格上传" width="800" append-to-body :before-close="handleClose">
    <div class="dialog-content">
      <ChatFileUpload :params="fileParams" :beforeUpload="beforeUpload">
        {{
          props.postDetail.category === "3"
            ? "类型为xlsx、xls的Excel文件,"
            : "类型为docx、doc、pdf的文件,"
        }}
      </ChatFileUpload>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import ChatFileUpload from "./chat-file-upload.vue";
import { UploadRawFile } from "element-plus";
import { KnowledgeFileQuery } from "@/api/ai/knowledge-file/types";
import { useUserStore } from "@/store";
import { ref } from "vue";

const dialogVisible = ref(false);
const props = defineProps({
  postDetail: {
    type: Object,
    default: () => { },
  },
});
const userStore = useUserStore();
const knowledgeId = ref<string>(userStore.user.sysUser?.id);
const emit = defineEmits(["updateVisible"]);
watch(
  () => props.postDetail,
  (newValue, oldValue) => {
    dialogVisible.value = newValue.show;
  },
  {
    deep: true,
  }
);
// 文件上传参数
const fileParams = ref<KnowledgeFileQuery>({
  category: props.postDetail.category, // 哪个菜单
  knowledgeId: knowledgeId.value, // 每个人独立一个文件库
});
// 关闭弹框并告知父组件
function handleClose() {
  dialogVisible.value = false;
  emit("updateVisible", dialogVisible);
}
function submit() {
  dialogVisible.value = false;
}
// 上传前事件
function beforeUpload(file: UploadRawFile) {
  const ext = file.name.substring(file.name.lastIndexOf(".") + 1);
  if (["doc", "docx", "pdf"].indexOf(ext) < 0) {
    ElMessage.warning("文件名只能为doc、docx、pdf");
    return false;
  }
  return true;
}
</script>
<style lang="scss" scoped>
.dialog-content {
  width: 90%;
  height: 360px;
  margin: 16px auto;
  border-radius: 7px 7px 7px 7px;
  border: 1px dashed #5d93fc;
  padding: 20px;
}
</style>
